<?php
/**
 * Created by PhpStorm.
 * User: huliren<huliren0516@163.com>
 * Date: 2017/11/6
 * Time: 9:54
 */

define('SITE_AUTH', 'mcode-manage-line');
require('../../../common.inc.php');

use lib\core\AdminAction;
use Center\MCode as Model;

class mcodeManage extends AdminAction
{
    public function doDefault()
    {
        $smarty = $this->app->page();

        $where = ['Id > ' => 0];

        $type = (isset($_GET['type']) and $_GET['type'] !== '') ? $_GET['type'] : null;

        if ($type !== null) {
            if ($type == 1) {
                $where += ['UserId >' => 0];
            } elseif ($type == 0) {
                $where += ['UserId' => 0];
            }
        }

        $smarty->value('s_data', [
            'type' => $type,
        ]);

        $page = empty($_GET['page']) ? 1 : (int)$_GET['page'];
        $limit = parent::PAGE_SIZE;
        $offset = ($page - 1) * $limit;

        $data = Model::getList($where, $offset, $limit);
        $count = $data['count'];
        if ($count > 0) {
            $page_info['page'] = ceil($count / $limit);
            $page_info['count'] = $count;

            $pagination = $this->pagination($count, $limit, $page);
            $smarty->value('page_info', $page_info);
            $smarty->value('pagination', $pagination);
            $smarty->value('list', $data['data']);
        }

        $smarty->output('user/mCode_list.tpl');
    }

    public function doRestore()
    {
        $id = $_GET['id'];
        $res = Model::update($id, ['UserId' => 0, 'Bluetooth' => '']);
        if ($res) {
            $this->output(['type' => 1]);
        } else {
            $this->output(['type' => 0]);
        }
    }
}

$app->run();